package com.xing.todolist.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.paging.DataSource;
import androidx.paging.Pager;
import androidx.paging.PagingConfig;
import androidx.paging.PagingData;
import androidx.paging.PagingLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xing.app.myutils.Utils.LogUtil;
import com.xing.todolist.R;
import com.xing.todolist.adapter.TodoListFragmentAdapter;
import com.xing.todolist.databinding.TodoListFragmentBinding;
import com.xing.todolist.db.base.AppDB;
import com.xing.todolist.db.base.TodoMsg;
import com.xing.todolist.db.dao.TodoMsgDao;
import com.xing.todolist.viewModel.TodoListFragmentVM;

import java.util.Random;

public class TodoListFragment extends Fragment {

    private TodoListFragmentBinding mBinding;
    private TodoListFragmentVM mViewModel;

    private TodoListFragmentAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = TodoListFragmentBinding.inflate(inflater, container, false);
        mViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(TodoListFragmentVM.class);
        mBinding.setData(mViewModel);
        mBinding.setLifecycleOwner(this);

        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TodoMsgDao dao = AppDB.getInstance(getContext()).getTodoMsgDao();

        mAdapter = new TodoListFragmentAdapter();
        mAdapter.setOnItemClickListener((position, todoMsg) -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(TodoEditFragment.DATA_KEY, todoMsg);

            Navigation.findNavController(mBinding.getRoot()).navigate(R.id.action_todoListFragment_to_todoEditFragment, bundle);
        });

        mBinding.recycleView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mBinding.recycleView.setAdapter(mAdapter);

        DataSource.Factory<Integer, TodoMsg> msgFactory = dao.findAllTodoMsg();

        mViewModel.setTodoMsgList(PagingLiveData.getLiveData(
                new Pager<Integer, TodoMsg>(
                        new PagingConfig(10, 10, true, 300),
                        null,
                        msgFactory.asPagingSourceFactory()
                )));
        mViewModel.getTodoMsgList().observe( getViewLifecycleOwner(), new Observer<PagingData<TodoMsg>>() {
            @Override
            public void onChanged(PagingData<TodoMsg> todoMsgPagingData) {
                mAdapter.submitData(getLifecycle(), todoMsgPagingData);
            }
        });

    }

    /**
     * 向数据库中插入数据
     */
    private static class AddDataAsync extends AsyncTask {

        private TodoMsgDao mDao;

        public AddDataAsync(TodoMsgDao dao) {
            mDao = dao;
        }

        @Override
        protected Object doInBackground(Object[] objects) {

            mDao.insert(new TodoMsg(System.currentTimeMillis() + "as" + new Random().nextInt(10)));

            LogUtil.e("插入了数据");
            return null;
        }

    }
}
